<!DOCTYPE html>
<html>

<head>
    <title>找回密码</title>
    <meta charset="utf-8">
    <meta name="viewport" content="initial-scale=1.0,maximum-scale=1,user-scalable=no">
    <meta content="yes" name="apple-mobile-web-app-capable">
    <meta content="telephone=no" name="format-detection">
    <link rel="stylesheet" type="text/css" href="/resource/web/css/reset.css">
    <link rel="stylesheet" type="text/css" href="/resource/web/css/forget-pwd.css">
    <script type="text/javascript" src="/resource/web/js/flexible.js"></script>
    <script type="text/javascript" src="/resource/web/js/jquery-1.11.1.min.js"></script>
</head>

<body>
    <div class="title">
        <p>找回密码</p>
        <a href="javascript:;" onClick="javascript :history.back(-1);"><img class="back" src="/resource/web/img/newhead/path_7_copy.png"></a>
    </div>
    <div class="pwd-log">
        <p class="pl-img"><img src="/resource/web/img/finacing/pwd.png"></p>
        <p class="pl-tit">找回密码</p>
    </div>
    <div class="pwd-list">
        <p><input type="text" name="phone" id="phone" placeholder="请输入手机号"></p>
        <p>
            <input type="number" name="captcha" id="captcha" placeholder="请输入验证码">
            <span class="get-num-btn" id="f_captcha">获取验证码</span>
        </p>
    </div>
    <div class="go-next-btn" onclick="SubmitMobile()">下一步</div>
<script type="text/javascript">
    //获取短信验证码
    $('#f_captcha').bind('click',function foo(){
        var mobile = $('#phone').val();
        var captcha = document.getElementById('captcha').value;
        if(check_mobile()){
            var wait = 60;
            var int = function fetch_captcha() {
                if (wait == 0) {
                    $("#f_captcha").bind("click", foo);
                    $('#f_captcha').html('获取短信验证码');
                    clearInterval(a)
                    wait = 60;
                }
                else {
                    $('#f_captcha').unbind("click", foo);
                    $('#f_captcha').html("重新发送(" + wait + ")");
                    wait--;
                }
            }
        var a = setInterval(int, 1000);
            $.ajax({
                url: "/get_news_captcha",
                type: "POST",
                data: "mobile=" + mobile,
                success: function (msg) {
                    if (msg['code'] == 0) {
                        //alert("验证码已发送,10分钟内输入有效");
                        $(".form-code span.code").removeAttr("disabled").css('cursor','pointer');//启用按钮
                        $(".form-code span.code").html("获取短信验证码");
                    }else if(!isNaN(msg)){
                        alert(""+msg+"秒后可重新发送手机短信验证码"+"");
                        window.clearInterval(a);//停止计时器
                        $(".form-code span.code").removeAttr("disabled").css('cursor','pointer');//启用按钮
                        $(".form-code span.code").html("获取短信验证码");
                        return false;
                    }else if(msg['code'] == 8||msg['code'] == 9||msg['code'] == 17||msg['code'] == 22){
                        alert("您发送验证码过于频繁");
                        window.clearInterval(a);//停止计时器
                        $(".form-code span.code").removeAttr("disabled").css('cursor','pointer');//启用按钮
                        $(".form-code span.code").html("获取短信验证码");
                        return false;
                    }else{
                        alert(""+msg['detail']+"");
                        alert("验证码发送有误,60秒后重新发送");
                        window.clearInterval(a);//停止计时器
                        $(".form-code span.code").removeAttr("disabled").css('cursor','pointer');//启用按钮
                        $(".form-code span.code").html("获取短信验证码");
                        return false;
                    }

                }
            });

        }
   })
 function check_mobile()
    {
        var mobile = $('#phone').val();
        var reg_mobile = /^1[34578]\d{9}$/
        var status = false;
        if(mobile){
            if (reg_mobile.test(mobile)) {
                var ajax = new XMLHttpRequest();
                ajax.onreadystatechange = function () {
                    if (ajax.readyState == 4) {
                        if (ajax.responseText == 1) {
                            status = true
                        } else {
                            alert('手机号未注册')
                        }
                    }
                }
                //与服务器建立连接
                ajax.open('post', '/js_verify_register', false);
                ajax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
                //处理请求
                ajax.send('mobile=' + mobile);
                return status
            } else {
                alert('请输入正确的手机号')
                return false
            }
        }else{
            alert('手机号不能为空')
            return false
        }

    }
    //提交
    function SubmitMobile(){
        var mobile  = $('#phone').val();
        var captcha = $('#captcha').val();
        $.ajax({
            url: 'web_forget.html',
            type: "post",
            data: {mobile: mobile, captcha: captcha},
            dataType: "json",
            timeout: 30000,
            success: function (json) {
                if (json.code == 1) {//ok
                    //alert(json.mess);
                    window.location.href = json.url;
                }
                if (json.code == 2) {//ok
                    alert(json.mess);
                }
            }
        });

    }


</script>
</body>

</html>
